<template>
  <div style="margin:30px">
    <el-form label-width="80px">
      <el-form-item label="讲师姓名">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <!-- 下拉选框的 value值如果和 下拉选项的value值相同 则选中下拉选项并回显label值 -->
        <el-select v-model="teacher.level" placeholder="请选择头衔">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>
      <!-- TODO:讲师头像  需要文件上传得到上传后的文件路径 -->
      <!-- TODO:讲师头像  需要文件上传得到上传后的文件路径 -->
      <el-form-item label="讲师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :on-error="handleAvastarError"
          :before-upload="beforeAvatarUpload"
          :action="uploadPath"
          class="avatar-uploader">
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="0" />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker
          v-model="teacher.joinDate"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default {
  // js代码：相当于在vue对象中写代码
  data() {
    return {
      uploadPath: process.env.BASE_API + '/admin/oss/upload?module=avatar', // 上传图片地址
      teacher: {
        level: 1,
        sort: 0
      }
    }
  },
  created() {
    if (this.$route.params.id) {
      // console.log(this.$route.params.id)
      teacherApi.getById(this.$route.params.id).then(r => {
        console.log(r.data)
        this.teacher = r.data.item
      })
    }
  },
  methods: {
    // 上传图片的回调函数
    // 上传头像失败的回调函数
    // 处理因为网络请求或者其他原因上传失败的情况
    handleAvastarError(err) { // 当响应报文状态码不为200时
      console.log(err)
      this.$message.error('网络连接失败!请稍后重试!')
    },

    // 上传成功的回调
    handleAvatarSuccess(res, file) {
      console.log(res)
      if (res.code === 20000) {
        this.$message.success('上传成功')
        this.teacher.avatar = res.data.path
        // 强制渲染
        this.$forceUpdate()
      } else {
        this.$message.error(res.message)
      }
    },
    // 上传之前的回调
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    updateById() {
      teacherApi.updateById(this.teacher).then(r => {
        console.log(this.teacher)
        this.$message.success('更新成功')
        this.$router.push('/teacher/list') // 指定路径如果能被路由匹配 则跳转到指定页面
      })
    },
    save() {
      // 提交新增请求
      teacherApi.save(this.teacher).then(r => {
        this.$message.success('新增成功')
        // 跳转到讲师列表页面
        // window.location = '' 跳转时会刷新页面
        // 使用路由跳转: 路由渲染的范围会刷新
        // this.$router 代表vue对象绑定的router对象
        this.$router.push('/teacher/list') // 指定路径如果能被路由匹配 则跳转到指定页面
      })
    },
    onSubmit() {
      if (this.teacher.id) {
        console.log('onsubmit' + this.teacher.id)
        // 表示讲师有id，更新
        this.updateById()
      } else {
        // 新增
        this.save()
      }
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
